package service

import (
	"MyBlogv2/common/HttpCode"
	define2 "MyBlogv2/common/define"
	"MyBlogv2/common/models"
	"MyBlogv2/common/response"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/copier"
	"strconv"
)

func AddComment(c *gin.Context) {
	var userCommentDto models.UserCommentDto
	var comment models.Comment
	userId, _ := c.Get("userId")

	if err := c.ShouldBind(&userCommentDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	if userCommentDto.ParentId == 0 {
		userCommentDto.ParentId = userCommentDto.ReplyId
	}
	copier.Copy(&comment, &userCommentDto)
	comment.CommentUserId = userId.(int)
	err := models.Db.Model(new(models.Comment)).Create(&comment).Error
	if err != nil {
		response.ResponseError(c, HttpCode.FAILURE)
		return
	}

	var user models.User
	models.Db.Model(new(models.User)).Where("id = ?", userId).First(&user)
	if user.Email == "" {
		response.ResponseOK(c, "检测到您尚未绑定邮箱,无法开启邮箱提醒，请先绑定邮箱")
	}
	response.ResponseOK(c, nil)

}

func GetCommentList(c *gin.Context) {
	typeOfComment := c.Query("type")
	typeId := c.Query("typeId")
	pageNum, _ := strconv.Atoi(c.DefaultQuery("pageNum", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
	var commentVoList []models.ArticleCommentVo
	var total int64
	models.Db.Model(new(models.Comment)).
		Table("t_comment c").
		Select("c.*,u.nickname as user_nickname,u.avatar user_avatar").
		Joins("left join sys_user u on c.comment_user_id = u.id").
		Where("type = ? and type_id = ? and is_check = ?", typeOfComment, typeId, define2.COMMENT_IS_CHECK).
		Where("parent_id = 0").
		Order("created_at desc").
		Count(&total).
		Offset((pageNum - 1) * pageSize).
		Limit(pageSize).
		Find(&commentVoList)

	for i, v := range commentVoList {
		var likeCount int64
		models.Db.Model(new(models.Like)).Where("type = ? and type_id = ?", define2.LIKE_TYPE_COMMENT, v.Id).Count(&likeCount)
		commentVoList[i].LikeCount = int(likeCount)
		var childCommentVoList []models.ArticleCommentVo
		models.Db.Model(new(models.Comment)).
			Table("t_comment c").
			Select("c.*,u.nickname as user_nickname,u.avatar user_avatar,ru.nickname as reply_user_nickname").
			Joins("left join sys_user u on c.comment_user_id = u.id").
			Joins("left join sys_user ru on c.reply_user_id = ru.id").
			Where("parent_id = ?", v.Id).
			Find(&childCommentVoList)

		for j, c := range childCommentVoList {
			var likeCountOfChildren int64
			models.Db.Model(new(models.Like)).Where("type = ? and type_id = ?", define2.LIKE_TYPE_COMMENT, c.Id).Count(&likeCountOfChildren)
			childCommentVoList[j].LikeCount = int(likeCountOfChildren)
		}
		commentVoList[i].ChildComment = childCommentVoList
	}
	response.ResponseOK(c, gin.H{
		"total": total,
		"page":  commentVoList,
	})
}

func GetCommentList4Admin(c *gin.Context) {
	var commentVoList []models.CommentListVo
	models.Db.Model(new(models.CommentListVo)).
		Table("t_comment c").
		Select("c.*,u.username as comment_username").
		Joins("left join sys_user u on c.comment_user_id = u.id").
		Find(&commentVoList)
	response.ResponseOK(c, commentVoList)
}

func SearchComment(c *gin.Context) {
	var searchCommentDto models.SearchCommentDto
	if err := c.ShouldBind(&searchCommentDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	db := models.Db.Model(new(models.CommentListVo)).
		Table("t_comment c").
		Select("c.*,u.username as comment_username").
		Joins("left join sys_user u on c.comment_user_id = u.id")
	if searchCommentDto.CommentContent != "" {
		db.Where("comment_content like ?", "%"+searchCommentDto.CommentContent+"%")
	}
	if searchCommentDto.CommentUserName != "" {
		db.Where("u.username like ?", "%"+searchCommentDto.CommentUserName+"%")
	}
	if searchCommentDto.Type.Valid {
		db.Where("type = ?", searchCommentDto.Type.Int64)
	}
	if searchCommentDto.IsCheck.Valid {
		db.Where("is_check = ?", searchCommentDto.IsCheck.Int64)
	}
	var commentVoList []models.CommentListVo
	db.Find(&commentVoList)
	response.ResponseOK(c, commentVoList)
}

func UpdateCommentIsCheck(c *gin.Context) {
	var commentIsCheckDto models.CommentIsCheckDto
	if err := c.ShouldBind(&commentIsCheckDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	models.Db.Model(new(models.Comment)).Where("id = ?", commentIsCheckDto.Id).Update("is_check", commentIsCheckDto.IsCheck)

	models.Db.Model(new(models.Comment)).Where("parent_id = ?", commentIsCheckDto.Id).Update("is_check", commentIsCheckDto.IsCheck)
	response.ResponseOK(c, nil)
}

func DeleteCommentById(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	models.Db.Model(new(models.Comment)).Where("id = ?", id).Or("parent_id = ?", id).Delete(new(models.Comment))
	response.ResponseOK(c, nil)
}
